import React, { useState, useEffect,useRef } from 'react'
import { Button, Table,Modal, Switch, message} from 'antd'
import axios from 'axios'
import { get } from '../../../utils/get'
import { put } from '../../../utils/put'

export default function User() {
    const [dataSource, setDataSource] = useState([])
    const [page,setPage]=useState(1)
    const [total,setTotal]=useState(0);
    const token = localStorage.getItem('TOKEN')
    useEffect(()=>{
        get(`/sysuser/list?page=${page}&limit=10`,token).then(res=>{
            console.log(res);
            if(res.data.code===0){
                setDataSource(res.data.page.list);
                setTotal(res.data.page.totalCount);
            }
            
        })
    },[page])
    const roleType =['admin','user'];
    const columns = [
        {
            title: '用户ID',
            dataIndex: 'resourceId',
            render: (resourceId) => {
                return <b>{resourceId}</b>
            }
        },
        {
            title: "加入时间",
            dataIndex: 'createTime',
        },
        {
            title: '用户昵称',
            dataIndex: 'nickname'
        },
        {
            title: "用户状态",
            dataIndex: 'status',
            render:(status,item)=>{
                return <Switch checked={status} onChange={()=>{handleChange(item)}} disabled={item.default}></Switch>
            }
        }
    ]
    const handleChange=(item)=>{//开关处理
        let newData = dataSource.map(i=>{
            if(i.resourceId===item.resourceId)
            {
                item.status =Number(!i.status); 
                put('/sysuser/update/status',{
                    userUid:i.resourceId,
                    status:i.status
                },token).then(res=>{
                   if(res.data.code===0)
                    message.success('修改成功')
                })
            }
            return i;
        })
        setDataSource([...newData])
    }
    return (
        <div>
            <Table dataSource={dataSource} columns={columns}
                pagination={{
                    pageSize: 10,
                    total:total,
                    onChange:(page)=>{
                        setPage(page);
                    }
                }}
                rowKey={item=>item.id}
                />
        </div>
    )
}
